const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const _ = db.command

exports.main = async (event, context) => {
  try {
    const { mealId } = event
    
    if (!mealId) {
      return {
        success: false,
        errMsg: '套餐ID不能为空'
      }
    }

    // 获取套餐基本信息
    const mealResult = await db.collection('meals')
      .doc(mealId)
      .get()

    if (!mealResult.data) {
      return {
        success: false,
        errMsg: '套餐不存在'
      }
    }

    const meal = mealResult.data

    // 获取食材详情
    const ingredientsResult = await db.collection('ingredients')
      .where({
        _id: _.in(meal.ingredientIds || [])
      })
      .get()

    // 获取营养师信息
    const nutritionistResult = await db.collection('nutritionists')
      .doc(meal.nutritionistId)
      .get()

    // 组装完整的套餐信息
    const fullMealData = {
      ...meal,
      ingredients: ingredientsResult.data || [],
      nutritionist: nutritionistResult.data || {}
    }

    return {
      success: true,
      data: fullMealData
    }

  } catch (error) {
    console.error('获取套餐详情失败:', error)
    return {
      success: false,
      errMsg: error.message || '获取套餐详情失败'
    }
  }
} 